:
eval 'exec perl -wS $0 ${1+"$@"}' if 0;

use File::Temp qw(tmpnam);

my $cue_file = shift @ARGV;


if (!open(CUE_FILE, "$cue_file")) {
	die("Cannot open the $cue_file");
};

my @cue_file_content = <CUE_FILE>;
close CUE_FILE;
my $global_performer = '';
my $tracks = 0;
my $genre = '';
my $year = '';
my $album = '';
my %song = (); # $song{$number} = \{$performer$title};
foreach (@cue_file_content) {
	chomp;
	$genre = clear_value($') and next if /^(REM\sGENRE)/;
	$year = clear_value($') and next if /^(REM\sDATE)/;
	$global_performer = clear_value($') and next if /^(PERFORMER)/;
	$album = clear_value($') and next if (/^(TITLE)/ && !$tracks);
};
        print ("\n$album $global_performer $year $genre\n");
exit(0);
sub clear_value {
	$value = shift;
	chomp $value;
	$value =~ s/^\s*\"\s*//;
	$value =~ s/\s*\"\s*$//;
	return $value;
}



print TMPFILE $dir;
close TMPFILE;
open (DIRNAME, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $new_dir_name = <DIRNAME>;
close DIRNAME;

opendir(DIRECTORY, $dir);
@dir_content = readdir(DIRECTORY);
my @new_dir_content;
closedir(DIRECTORY);
open(TMPFILE, ">$tmp_file");
foreach (@dir_content) {
    chomp;
    next if /^\.+$/;
    push(@new_dir_content, $_);
    print TMPFILE $_ . "\n";
};
close TMPFILE;

open (DIRCONTENT, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $i = 0;
foreach (<DIRCONTENT>) {
#     print $_;
     chomp;
     my $old_path = $dir . '/' . $new_dir_content[$i];
     my $new_path = $dir . '/' . $_;
#     print "Rename $dir/". $new_dir_content[$i] . " to $dir/$_\n";
     rename($old_path, $new_path) or system("mv", $old_path, $new_path);
     $i++;     
};
close DIRCONTENT;

rename($dir, $new_dir_name) or system("mv", $dir, $new_dir_name);

unlink $tmp_file;
